package com.scvtc.service.student.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.student.base.StudentsBaseDto;
import com.scvtc.domain.entity.student.StudentAwards;
import com.scvtc.domain.entity.student.StudentsBase;
import com.scvtc.domain.enums.OpType;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.domain.vo.student.base.StuEnrollmentPlanVo;
import com.scvtc.mapper.student.StudentsBaseMapper;
import com.scvtc.service.student.StudentAwardsService;
import com.scvtc.service.student.StudentsBaseService;
import com.scvtc.utils.BeanCopyUtils;
import com.scvtc.utils.DictionaryUtil;
import com.scvtc.utils.ExportExcelUtil;
import com.scvtc.utils.LikeQueryUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.scvtc.constants.ExportExcelConstants.*;
import static com.scvtc.constants.RedisConstant.STUDENT_BASE;


/**
 * 学生基本信息(StudentsBase)表服务实现类
 *
 * @author yiFei
 * @since 2022-04-13 17:38:27
 */
@Service("studentsBaseService")
public class StudentsBaseServiceImpl extends ServiceImpl<StudentsBaseMapper, StudentsBase> implements StudentsBaseService {

    @Autowired
    private StudentAwardsService studentAwardsService;

    @Autowired
    private DictionaryUtil dictionaryUtil;

    /**
     * 获取学生基本信息
     *
     * @param current
     * @param size
     * @param studentsBaseDto
     * @return
     */
    @Override
    public ResResult<PageVo<List<StudentsBaseDto>>> getAllStudentBase(
            Long current, Long size,
            StudentsBaseDto studentsBaseDto) {
        // 查询学生信息
        Page<StudentsBase> page = page(new Page<>(current, size),
                LikeQueryUtil.likeQuery(studentsBaseDto, StudentsBase.class)
                        .lambda()
                        .orderByDesc(StudentsBase::getCreateTime)
        );
        // 封装 Bean
        List<StudentsBase> records = page.getRecords();
        List<StudentsBaseDto> studentBaseList = BeanCopyUtils.copyBean(records, StudentsBaseDto.class);
        long total = page.getTotal();
        return ResResult.noMsgSuccess(new PageVo<>(studentBaseList, total));
    }

    /**
     * 添加一个学生
     *
     * @param studentsBaseDto
     * @return
     */
    @SneakyThrows
    @Override
    public ResResult<?> addStudentBase(StudentsBaseDto studentsBaseDto) {
        // 不需要传入 id
        StudentsBase studentsBase = BeanCopyUtils.copyBean(studentsBaseDto, StudentsBase.class).setId(null);
        if (save(studentsBase)) {
            dictionaryUtil.refreshData(studentsBase, STUDENT_BASE, OpType.SAVE);
            return ResResult.success(studentsBase.getId());
        }
        return ResResult.fail();
    }

    /**
     * 修改学生信息
     *
     * @param studentsBaseDto
     * @return
     */
    @Override
    public ResResult<?> updateStudentBase(StudentsBaseDto studentsBaseDto) {
        // 查询原来学生如果信息重复就提示错误
        StudentsBase studentsBase = BeanCopyUtils.copyBean(studentsBaseDto, StudentsBase.class);
        // 修改失败
        if (updateById(studentsBase)) {
            dictionaryUtil.refreshData(studentsBase, STUDENT_BASE, OpType.UPDATE);
            return ResResult.success();
        }
        // 发回信息错误
        return ResResult.fail();
    }

    /**
     * 删除单个学生
     *
     * @param id
     * @return
     */
    @Override
    public ResResult<?> deleteStudentBase(Long id) {
        // 删除奖状
        studentAwardsService.remove(
                new LambdaQueryWrapper<StudentAwards>()
                        .eq(StudentAwards::getStuId, id)
        );
        // 删除学生基本信息
        if (removeById(id)) {
            dictionaryUtil.refreshData(new StudentsBase().setId(id), STUDENT_BASE, OpType.UPDATE);
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除多个学生
     *
     * @param ids
     * @return
     */
    @Override
    public ResResult<?> deleteMultipleStudentBase(Long[] ids) {
        studentAwardsService.remove(
                new LambdaQueryWrapper<StudentAwards>()
                        .in(StudentAwards::getStuId, Arrays.asList(ids))
        );
        removeByIds(Arrays.asList(ids));
        dictionaryUtil.deleteMultiple(ids, STUDENT_BASE, OpType.DELETE_MULTIPLE);
        return ResResult.success();
    }

    @Override
    public void exportExcel(HttpServletResponse response, StudentsBaseDto studentsBaseDto) {

        // 先查询
        List<StudentsBase> list = list(LikeQueryUtil.likeQuery(studentsBaseDto, StudentsBase.class));

        // BeanCopy 拷贝
        List<StudentsBaseDto> studentsBaseList = BeanCopyUtils.copyBean(list, StudentsBaseDto.class);
        // 使用 ExportExcelUtil 导出 Excel
        ExportExcelUtil.exportExcel(
                new ExportParams(STUDENTS_BASE_TITLE, STUDENTS_BASE_SHEET),
                StudentsBaseDto.class,
                studentsBaseList,
                response,
                STUDENT_BASE_FILENAME
        );
    }

    @Override
    public void exportExcel(HttpServletResponse response,Long[] ids) {
        List<StudentsBase> list=list(
                new LambdaQueryWrapper<StudentsBase>()
                        .in(StudentsBase::getId, Arrays.asList(ids))
        );
        // BeanCopy 拷贝
        List<StudentsBaseDto> studentsBaseList = BeanCopyUtils.copyBean(list, StudentsBaseDto.class);
        // 使用 ExportExcelUtil 导出 Excel
        ExportExcelUtil.exportExcel(
                new ExportParams(STUDENTS_BASE_TITLE, STUDENTS_BASE_SHEET),
                StudentsBaseDto.class,
                studentsBaseList,
                response,
                STUDENT_BASE_FILENAME
        );
    }

    /**
     * 学生男女比例 (echarts 图)
     */
    @Override
    public ResResult<List<Map<String, Object>>> chartGender() {
        List<Map<String, Object>> chartGender = getBaseMapper().chartGender();
        return ResResult.noMsgSuccess(chartGender);
    }

    /**
     * 学生近三年招生(echarts 图)
     */
    @Override
    public ResResult<List<StuEnrollmentPlanVo>> chartEnrollmentPlan() {
        List<StuEnrollmentPlanVo> chartEnrollmentPlan = getBaseMapper().chartEnrollmentPlan(LocalDateTime.now().getYear());
        return ResResult.noMsgSuccess(chartEnrollmentPlan);
    }

}

